<?php

/**
 * .<p/>
 * @catepory 【核心】
 * @author zhouhaichao(a)2008.sina.com
 * @version 1.0
 * @date: 2015/11/18
 * @time: 21:00
 */

class SmartyViewRenderer extends ViewRenderer
{
    public function display()
    {
        $config = Config::getInstance();
        $request = $this->request;

        $smarty = new Smarty;
        $smarty->debugging = $config->getConfig(array("smarty","debug"), $config->getConfig("debug", false));
        $smarty->caching = $config->getConfig(array("smarty", "caching"), true);
        $smarty->cache_lifetime = $config->getConfig(array("smarty", "cache_lifetime"), 120);

        $smarty->setTemplateDir($config->getConfig(array("smarty", "template_dir"), ROOT . "/application/view"));
        $smarty->setCompileDir($config->getConfig(array("smarty", "compile_dir"), ROOT . "/tmp/compile"));
        $smarty->setCacheDir($config->getConfig(array("smarty", "cache_dir"), ROOT . "/tmp/cache"));
        $smarty->setConfigDir($config->getConfig(array("smarty", "config_dir"), ROOT . "/application/config"));

        $smarty->assign("req", $request);
        $smarty->assign("conf", $config);
        $smarty->assign("param", $request->params);

        foreach ($request->attrs as $key => $value) {
            if(in_array($key,array("req","conf","param"))){
                throw new Exception("Can't set attr named 'req' or 'conf' or 'param' !");
            }else{
                $smarty->assign($key, $value);
            }
        }

        $smarty->display($this->view);
    }
}